<?php /* $Id: view.php,v 1.89.4.5 2006/11/22 19:35:14 gregorerhardt Exp $ */
$task_id = intval( dPgetParam( $_GET, "task_id", 0 ) );
$task_log_id = intval( dPgetParam( $_GET, "task_log_id", 0 ) );
$reminded = intval( dPgetParam( $_GET, 'reminded', 0) );


// check permissions for this record
$canRead = !getDenyRead( $m, $task_id );
$canEdit = !getDenyEdit( $m, $task_id );
// check permissions for this record
$canReadModule = !getDenyRead( $m );

if (!$canRead) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
$perms =& $AppUI->acl();
$canViewTaskFee = $perms->checkModuleItem( "taskfee", 'view');
$canViewExpense = $perms->checkModuleItem('task_expense', 'view');
// trangdt add
$hasRoleFfifaMana = $perms->userHasRole($AppUI->user_id, 'FFIA_Management');
// trangdt end

$q =& new DBQuery;
$q->addTable('tasks');
$q->leftJoin('md_unit', 'u', 'tasks.task_unit = u.unit_id');
$q->leftJoin('users', 'u1', 'u1.user_id = task_owner');
$q->leftJoin('projects', 'p', 'p.project_id = task_project');
$q->leftJoin('task_log', 'tl', 'tl.task_log_task = task_id');
$q->addWhere('task_id = ' . $task_id);
$q->addQuery('tasks.*');
$q->addQuery('u.unit_name');
$q->addQuery('project_name, project_color_identifier, project_owner');
$q->addQuery('u1.user_username as username');
$q->addQuery('ROUND(SUM(task_log_hours),2) as log_hours_worked');
$q->addGroup('task_id');

// check if this record has dependencies to prevent deletion
$msg = '';
$obj = new CTask();
$canDelete = $obj->canDelete( $msg, $task_id );
$sql = $q->prepare();
$q->clear();

if (!db_loadObject( $sql, $obj, true, false )) {
	$AppUI->setMsg( 'Task' );
	$AppUI->setMsg( 'invalidID', UI_MSG_ERROR, true );
	$AppUI->redirect();
} else {
	$AppUI->savePlace();
}

if (!$obj->canAccess( $AppUI->user_id )) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

require_once($AppUI->getModuleClass('items'));
$itemO = new CItem();
$itemO->load($obj->task_item);

$canEdit = $obj->task_is_complete == 0 && ($hasRoleFfifaMana||($canEdit && $obj->task_costcenter == $AppUI->user_costcenter));
// Clear any reminders
if ($reminded)
	$obj->clearReminder();

// retrieve any state parameters
if (isset( $_GET['tab'] )) {
	$AppUI->setState( 'TaskLogVwTab', $_GET['tab'] );
}
$tab = $AppUI->getState( 'TaskLogVwTab' ) !== NULL ? $AppUI->getState( 'TaskLogVwTab' ) : 0;

// get the prefered date format
$df = $AppUI->getPref('SHDATEFORMAT');
//Also view the time
//$df .= ' ' . $AppUI->getPref('TIMEFORMAT');

$start_date = intval( $obj->task_start_date ) ? new CDate( $obj->task_start_date ) : null;
$end_date = intval( $obj->task_end_date ) ? new CDate( $obj->task_end_date ) : null;

//check permissions for the associated project
$canReadProject = !getDenyRead( 'projects', $obj->task_project);

$amounts = $obj->getTaskLogAmounts($obj->task_id);

// get the users on this task
$q->addTable('users', 'u');
$q->addTable('user_tasks', 't');
$q->leftJoin('contacts', 'c' , 'user_contact = contact_id');
$q->addQuery('u.user_id, u.user_username, contact_email, t.perc_assignment');
$q->addQuery('CONCAT(c.contact_first_name, " ", c.contact_last_name) AS contact_name');
$q->addWhere('t.task_id = ' . $task_id);
$q->addWhere('t.user_id = u.user_id');
$q->addOrder('u.user_username');
$users = $q->loadList();

$efforts = 0.0;
foreach ($users as $u) {
	$efforts += $obj->task_duration * $u['perc_assignment'] / 100;
}
$isMyProject = ($AppUI->user_id == $obj->project_owner);
$isMyTask = $isMyProject || ($AppUI->user_id == $obj->task_owner);

if (!$isMyTask) {
	foreach ($users as $u) {
		if ($u['user_id'] == $AppUI->user_id) {
			$canAddSubTask = true;
			break;
		}
	}
}
$durnTypes = dPgetSysVal( 'TaskDurationType' );

// setup the title block
$titleBlock = new CTitleBlock( 'View Task' );
$titleBlock->addButton( $AppUI->_('tasks list'), 'index.php?m=projects&a=view&tab=0&project_id='.$obj->task_project );
/*if ($canEdit) {
	if ($isMyTask || $canAddSubTask) {
		$titleBlock->addButton( $AppUI->_('add sub task'),
			'index.php?m=tasks&a=simpleaddedit&task_project='.$obj->task_project.'&task_parent='.$task_id);
	}
}*/
$titleBlock->addCell();
$titleBlock->show();

$task_types = dPgetSysVal('TaskType');

?>

<script language="JavaScript">
<? if ($canReadProject) { ?>
function viewProject() {
	go('?m=projects&a=view&project_id=<?=$obj->task_project?>');
}
<? } else { ?>
function viewProject() {}
<? }

if ($canEdit) {
?>

function updateTask() {
	var f = document.editFrm;
	if (f.task_log_description.value.length < 1) {
		alert( "<?=$AppUI->_('tasksComment', UI_OUTPUT_JS)?>" );
		f.task_log_description.focus();
	} else if (isNaN( parseInt( f.task_percent_complete.value+0 ) )) {
		alert( "<?=$AppUI->_('tasksPercent', UI_OUTPUT_JS)?>" );
		f.task_percent_complete.focus();
	} else if(f.task_percent_complete.value  < 0 || f.task_percent_complete.value > 100) {
		alert( "<?=$AppUI->_('tasksPercentValue', UI_OUTPUT_JS)?>" );
		f.task_percent_complete.focus();
	} else {
		f.submit();
	}
}

function doTaskEdit() {
	go('index.php?m=tasks&a=simpleaddedit&task_id=<?=$task_id?>');
}

function doTaskDelete() {
	if (confirm( "<?=$AppUI->_('doDelete', UI_OUTPUT_JS).' '.$AppUI->_('Task', UI_OUTPUT_JS).'?'?>" )) {
		document.frmDelete.submit();
	}
}

function doTaskComplete() {
	go('?m=tasks&a=complete&task_id=<?=$task_id?>');
}
<? } else { ?>
function doTaskEdit() {}
function doTaskDelete() {}
function doTaskComplete() {}
<? } ?>
</script>
<form name="frmDelete" action="index.php?m=tasks" method="post">
	<input type="hidden" name="dosql" value="do_task_aed">
	<input type="hidden" name="del" value="1" />
	<input type="hidden" name="task_id" value="<?=$task_id?>" />
</form>
<table border="0" cellpadding="4" cellspacing="0" width="100%" class="smallinfopanel">
<tr>
	<th colspan="100%" style="cursor: default" class="last">
		<table border="0" cellpadding="0" cellspacing="1" width="100%">
			<tr>
				<td><img src="images/<?=$obj->task_milestone ? 'milestone.gif' : 'task.gif'?>" border="0"
						title="<?=$obj->task_milestone ? 'Milestone' : 'Task'?>"/></td>
				<td width="100%" style="color: white">&nbsp;
					<?php
						echo '<span onclick="viewProject()" class="clickable" title="View project">'.
								'[ '.@$obj->project_name.' ]'.
							 '</span> ';
						echo @$obj->task_name;
					?>
				</td>
			<? if ($canEdit && $obj->task_is_complete < 1) { ?>
				<td onclick="doForceCompleteTask('<?=$obj->task_id?>', '<?=addslashes($obj->task_name)?>')" class="clickable">
					<img src="images/force_complete.gif" border="0" title="Force complete"/>
				</td>
				<td onclick="doTaskEdit()" style="padding-left: 4px" class="clickable">
					<img src="images/edit_task.gif" border="0" title="Edit task"/>
				</td>
			<? } ?>
			<? if ($canDelete && $canEdit) { ?>
				<td onclick="doTaskDelete()" style="padding-left: 4px" class="clickable">
					<img src="images/delete_task.gif" border="0" title="Delete task"/>
				</td>
			<? } ?>
			</tr>
		</table>
	</th>
</tr>
<tr valign="top">
	<td width="20%">
		<table cellspacing="0" cellpadding="3" border="0" width="100%" class="list">
			<tr class="info">
				<td nowrap="nowrap" width="20%">
					<?=$AppUI->_('Quantity')?>
				</td>
				<td nowrap="nowrap">
					<?=number_format($obj->task_figure)?>
				</td>
			</tr>
			<tr class="second">
				<td align="right" nowrap="nowrap"><?=$AppUI->_('Unit')?></td>
				<td><?=$obj->unit_name?></td>
			</tr>
			<!--
			<tr class="second">
				<td align="right" nowrap="nowrap"><?=$AppUI->_('Quantity unit')?></td>
				<td><?=$itemO->qunit_name?></td>
			</tr>
			<tr class="second">
				<td align="right" nowrap="nowrap"><?=$AppUI->_('Weight unit')?></td>
				<td><?=$itemO->wunit_name?></td>
			</tr>
			<tr class="second">
				<td align="right" nowrap="nowrap" valign="top"><?=$AppUI->_('Volumne unit')?></td>
				<td><?=$itemO->vunit_name?></td>
			</tr>-->
		</table>
	</td>
	<td width="40%">
		<table cellspacing="0" cellpadding="3" border="0" width="100%" class="list">
			<tr class="info">
				<td nowrap="nowrap" colspan="2">
					<?=$AppUI->_('Progression').':&nbsp;&nbsp;<span id="idPercent">'.@($obj->task_is_complete == 0 ? (@$obj->task_percent_complete . '%') : '<font color="Green">Completed</font>').'</span>'?>
				</td>
				<?php if ($canViewExpense):?>
				<td nowrap="nowrap" align="right">
				<?=$AppUI->_('Expense Amount')?>
				</td>
				<td><?=number_format($amounts['sum'])?></td>
				<?php endif;?>
			</tr>
			<tr class="second">
				<td align="right" nowrap="nowrap"><?=$AppUI->_('Start Date')?></td>
				<td><?=$obj->task_start_time . ' : ' . ($start_date ? $start_date->format( $df ) : '-')?></td>
				<?php if ($canViewExpense):?>
				<td align="right" nowrap="nowrap"><?=$AppUI->_('Inter expense')?></td>
				<td><?=number_format($amounts['interExpense'])?></td>
				<?php endif;?>
			</tr>
			<tr class="second">
				<td align="right" nowrap="nowrap"><?=$AppUI->_('Finish Date')?></td>
				<td><?=$obj->task_end_time . ' : ' . ($end_date ? $end_date->format( $df ) : '-')?></td>
				<?php if ($canViewExpense):?>
				<td align="right" nowrap="nowrap"><?=$AppUI->_('On behalf expense')?></td>
				<td><?=number_format($amounts['behalfExpense'])?></td>
				<?php endif;?>
			</tr>

		<?php if ($canViewTaskFee) {?>
			<tr class="second">
				<td align="right" nowrap="nowrap" valign="top"><?=$AppUI->_('Sales amount')?></td>
				<td>
					<?
					 	$obj->task_fee = $obj->task_fee == -1 ? 0 : $obj->task_fee;
						 echo number_format($obj->task_fee)
					?>
				</td>
				<?php if ($canViewExpense):?>
				<td align="right" nowrap="nowrap"><?=$AppUI->_('Other expense')?></td>
				<td><?=number_format($amounts['otherExpense'])?></td>
				<?php endif;?>
			</tr>
		<?php }?>


		</table>
	</td>
	<td width="40%">
		<table width="100%" cellspacing="0" cellpadding="3" border="0" class="list">
		<tr class="info">
			<td nowrap="nowrap" colspan="2" ><?=$AppUI->_('Details')?></td>
		</tr>
		<?php if ($obj->task_quotation > 0) {
			require_once($AppUI->getModuleClass('quotations'));
			$quote = new CQuotations();
			$quote->load($obj->task_quotation);
			}
		?>
		<tr class="second">
			<td align="right" nowrap="nowrap" width="20%"><?=$AppUI->_('Quotation')?></td>
			<td >
				<?php if ($quote->quote_id > 0) {?>
					<a href="<?='index.php?m=quotations&a=quote_view&quote_id='.@$quote->quote_id?>"><b><?=@$quote->quote_name?></b></a>
				<?php } else '<b>' . $AppUI->_('not link quotation') . '</b>'?>
			</td>
		</tr>
		<tr class="second">
			<td align="right" valign="top"><?=$AppUI->_('Description')?></td>
			<td style="height: 48px"  valign="top">
				<?=str_replace( chr(10), '<br/>', $obj->task_description)?>
			</td>
		</tr>
		</table>
	</td>

	<!--
	<td width="50%">
		<table cellspacing="0" cellpadding="3" border="0" width="100%" class="list">
		<tr class="info">
			<td><?=$AppUI->_('Assigned Users')?></td>
		</tr>
		<?php
			echo count( $users ) == 0 ? '<tr class="second"><td>&nbsp;</td></tr>' : '';
			foreach($users as $row) {
		?>
		<tr class="second">
			<td style="padding-left: 8px">
				<div style="float: left">
				<img src="images/mail.gif" border="0">&nbsp;
				<a href="mailto:<?=$row['contact_email']?>"><?=$row['contact_name']?> &lt;<?=$row['contact_email']?>&gt;</a>
				</div>
				<div style="float: right"><?=$row['perc_assignment']?>%&nbsp;</div>
			</td>
		</tr>
		<?
				if ($row['user_id'] == $AppUI->user_id) {
					$isMyTask = true;
				}
			}
		?>

		<?php
			// Pull tasks dependencies
			$q->addQuery('td.dependencies_req_task_id, t.task_name');
			$q->addTable('tasks', 't');
			$q->addTable('task_dependencies', 'td');
			$q->addWhere('td.dependencies_req_task_id = t.task_id');
			$q->addWhere('td.dependencies_task_id = ' . $task_id);

			$taskDep = $q->loadHashList();
			$q->clear();
		?>
		<tr class="info">
			<td><?=$AppUI->_('Predecessors')?></td>
		</tr>
		<?php
			echo count( $taskDep ) == 0 ? '<tr class="second"><td>&nbsp;</td></tr>' : '';
			foreach ($taskDep as $key => $value) {
				echo '<tr class="second">'.
					'<td style="padding-left: 8px"><img src="images/task_rel.gif" border="0">&nbsp;' .
					'<a href="./index.php?m=tasks&a=view&task_id='.$key.'">'.$value.'</a>' .
					'</td></tr>';
			}
		?>
        <?php
			// Pull the tasks depending on this Task
			$q->addQuery('td.dependencies_task_id, t.task_name');
			$q->addTable('tasks', 't');
			$q->addTable('task_dependencies', 'td');
			$q->addWhere('td.dependencies_task_id = t.task_id');
			$q->addWhere('td.dependencies_req_task_id = ' . $task_id);
			$dependingTasks = $q->loadHashList();
			$q->clear();
		?>
		<tr class="info">
			<td><b><?=$AppUI->_('Followers')?></b></td>
		</tr>
		<?php
			echo count( $dependingTasks ) == 0 ? '<tr class="second"><td>&nbsp;</td></tr>' : '';
			foreach($dependingTasks as $key => $value) {
				echo '<tr class="second">'.
					'<td style="padding-left: 8px"><img src="images/task_rel.gif" border="0">&nbsp;' .
					'<a href="index.php?m=tasks&a=view&task_id='.$key.'">'.$value.'</a>' .
					'</td></tr>';
			}

		/*
		 	require_once  $AppUI->getSystemClass( 'CustomFields' );
			$custom_fields = New CustomFields( $m, $a, $obj->task_id, "view" );
			$custom_fields->printHTML();
		*/
		?>
		</table>
	</td>
	-->
</tr>
</table>

<?php
$tabBox = new CTabBox( "?m=tasks&a=view&task_id=$task_id", $tab );
$tabBox_show = 0;
if ( $obj->task_dynamic != 1 ) {
	// tabbed information boxes
	if ($perms->checkModuleItem('task_log', 'view', $task_id)) {
		$tabBox_show = 1;
		$tabBox->add( 'vw_logs', 'Task Logs' );
		// fixed bug that dP automatically jumped to access denied if user does not
		// have read-write permissions on task_id and this tab is opened by default (session_vars)
		// only if user has r-w perms on this task, new or edit log is beign showed
//		if ($isMyTask) {
//	        if ($task_log_id == 0) {
//	            if ($perms->checkModuleItem('task_log', 'add', $task_id))
//	                $tabBox->add( 'vw_log_update', 'New Log' );
//	        }
//	        else if ($perms->checkModuleItem('task_log', 'edit', $task_id)) {
//	            $tabBox->add( 'vw_log_update', 'Edit Log' );
//			}
//		}
	}
}

if ( count($obj->getChildren()) > 0 ) {
	// Has children
	// settings for tasks
	$f = 'children';
	$min_view = true;
	$tabBox_show = 1;
	// in the tasks file there is an if that checks
	// $_GET[task_status]; this patch is to be able to see
	// child tasks withing an inactive task
	$_GET['task_status'] = $obj->task_status;
	$tabBox->add( 'tasks', 'Child Tasks' );
}
$tabBox->loadExtras($m, $a);
$tabBox->add( 'file', 'Files' );

if ($isMyTask && $tabBox->loadExtras($m, $a))
	$tabBox_show = 1;

if ( $tabBox_show == 1)	$tabBox->show();
?>
<script language="JavaScript" type="text/javascript">
  function doForceCompleteTask(id, name)
	{
		if (confirm( "Do you want to force complete task: "+ name)) {
			location.href = "index.php?m=tasks&a=do_force_complete&task_id=" + id;
		}
	}
</script>
